################################################################################
#
#                 Bringing Home the Bacon (World Politics)
#
#                               Kim & Fu  
#
#                              August 2024
#
################################################################################

library(tidyverse)
library(ggthemes)
library(lubridate)
library(stringr)
library(lfe)
library(stargazer)
library(treemapify)

################# PART 2: MAIN ANALYSIS #################

# set working directory
setwd("~/")

load("export_ambassador.RData")

# DATA

## Figure 1: Top 30 Export Markets of the United States, 2002-2020
exp30 %>%
  ggplot(aes(area = total_exp, fill = as.factor(top30_y), label = country)) +
  geom_treemap() +
  geom_treemap_text(colour="black", grow=T, reflow=T, place="centre") +
  theme(legend.position = "none") +
  scale_fill_manual(values = c("grey91", "skyblue")) 


# THE CASE OF TERRY BRANSTAD: Smoking-gun Evidence

## Figure 2
export_ambassador_cst %>%
  filter(country == "China" & year >= 2016) %>%
  mutate(iowa = ifelse(state == "Iowa", "Iowa", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, iowa) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  ggplot(aes(x = year, y = log_export, color = iowa)) +
  geom_vline(xintercept = 2017.5, linetype = 2) +
  geom_line(size = 1.1, alpha = 0.8) +
  geom_point(size = 2.5, alpha = 0.8) +
  scale_color_manual(values = c("blue2", "grey40")) +
  annotate("text", x = 2017, y = 18.3, label = "Branstad's\nTerm Begins") +
  labs(x = "Year", y = "Average Exports in Log", color = "") +
  theme_few()


export_ambassador %>%
  filter(naics == "311 Food & Kindred Products") %>%
  filter(country == "China" & year >= 2016) %>%
  mutate(iowa = ifelse(state == "Iowa", "Iowa", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, iowa) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  ggplot(aes(x = year, y = log_export, color = iowa)) +
  geom_vline(xintercept = 2017.5, linetype = 2) +
  geom_line(size = 1.1, alpha = 0.8) +
  geom_point(size = 2.5, alpha = 0.8) +
  scale_color_manual(values = c("blue2", "grey40")) +
  annotate("text", x = 2017, y = 16.5, label = "Branstad's\nTerm Begins") +
  labs(x = "Year", y = "Average Exports in Log", color = "") +
  theme_few()


## Figure 3
initial <- export_ambassador_cst %>%
  mutate(year_month = as.numeric(paste0(year, ".", month))) %>%
  filter(year_month == year_month_start) %>%
  filter(state == home_state) %>%
  group_by(country, firstname, lastname, type) %>%
  summarize(export_initial = sum(total_export_value, na.rm=T)) %>%
  mutate(log_export_initial = log(export_initial))

later <- export_ambassador_cst %>%
  mutate(year_month = as.numeric(paste0(year, ".", month)))%>%
  mutate(yr2_after  = year_month_start + 2) %>%
  filter(year_month == yr2_after) %>%
  filter(state == home_state) %>%
  group_by(country, firstname, lastname, type) %>%
  summarize(export_later = sum(total_export_value, na.rm=T)) %>%
  mutate(log_export_later = log(export_later)) 

initial_later <- initial %>%
  inner_join(later) %>%
  mutate(d_export_2y = export_later - export_initial,
         d_export_2y_pct = d_export_2y/export_initial*100)%>%
  mutate(branstad = ifelse(lastname == "Branstad", "Branstad", NA)) %>%
  mutate(branstad2 = ifelse(lastname == "Branstad", "Branstad", "Others"))

initial_later %>%
  filter(d_export_2y_pct <= 125) %>%
  ggplot(aes(x = type, y = d_export_2y_pct)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(aes(color = branstad2, shape = branstad2, alpha = branstad2), width = 0.1, size = 2, show.legend = F) + 
  geom_text(aes(label = branstad), nudge_y = 0, nudge_x = 0.3, show.legend = F) +
  geom_hline(yintercept = 0, lty = "dashed") +
  scale_color_manual(values = c("darkred", "blue")) +
  scale_shape_manual(values = c(8, 16)) +
  scale_alpha_manual(values = c(0.8, 0.5)) +
  scale_x_discrete(labels = c("Diplomat", "Non-Politician", "Politician")) +
  labs(x="Ambassador Type", 
       title = "", #title="Changes in Home-State Export by Ambassador Type",
       y="Changes in Home-State Export After Two Years (%)") +
  theme_few()



# EMPIRICAL ANALYSIS: Country-State-Time Level 

### Main Results ###

## Table 1: Home-State Effect and Ambassador Types
mod.main.cst  <- felm(log_export ~ ambassador_home_state | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst,  weights = export_ambassador_cst$wt_cs)
mod.main.type.cst <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst, weights = export_ambassador_cst$wt_cs)

stargazer(mod.main.cst, mod.main.type.cst, 
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes")))

## Table 2: Home-State Effect Across Different Cutoffs of Export Partners
mod.main.type.cst.5   <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 5,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 5,]$wt_cs)
mod.main.type.cst.10  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 10,]$wt_cs)
mod.main.type.cst.15  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 15,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 15,]$wt_cs)
mod.main.type.cst.20  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 20,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 20,]$wt_cs)
mod.main.type.cst.25  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 25,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 25,]$wt_cs)
mod.main.type.cst.30  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 30,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 30,]$wt_cs)

stargazer(mod.main.type.cst.5, mod.main.type.cst.10, mod.main.type.cst.15, 
          mod.main.type.cst.20, mod.main.type.cst.25, mod.main.type.cst.30,
          type = "text",
          column.labels = c("Top 5", "Top 10", "Top 15", "Top20", "Top 25", "Top 30"),
          keep = c("ambassador_home_stateXdiplomat", "ambassador_home_stateXpolitician", "ambassador_home_stateXnon_politician"),
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")))


### Home-State Effect by Industry ###

mod_naics <- export_ambassador %>%
  group_by(naics) %>%
  summarize(sector_export_value = sum(total_export_value),
            sector_log_export = log(sector_export_value + 1)) %>%
  arrange(desc(sector_export_value)) %>%
  mutate(rank_naics = 1:35) %>%
  mutate(coef = 0, se = 0) %>%
  filter(naics != "990 Other Special Classification Provisions" &
           naics != "980 Goods Returned (exports For Canada Only)" &
           naics != "920 Used Or Second-hand Merchandise" &
           naics != "930 Used Or Second-hand Merchandise" & 
           naics != "115 Products Supporting Agriculture And Forestry")

naics <- unique(mod_naics$naics)
for (i in 1:30) {
  mod <- felm(log_export ~ ambassador_home_stateXpolitician + 
                ambassador_home_stateXnon_politician + 
                ambassador_home_stateXdiplomat  | 
                countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, 
              data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[i],],
              weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[i ],]$wt_csi)
  mod_naics[mod_naics$naics == naics[i], 5] <- mod$coefficients[1,]
  mod_naics[mod_naics$naics == naics[i], 6] <- mod$cse[[1]]
  rm(mod)
}


## Figure 4: Politician Ambassador’s Home-State Effect by Industry
mod_naics %>%
  mutate(signif_products = ifelse(coef - 1.95 * se > 0, "1", "0")) %>%
  mutate(naics = reorder(naics, coef)) %>%
  ggplot(aes(x = coef, y = naics, color = signif_products, shape = signif_products)) +
  geom_vline(xintercept = 0, color = "grey40") +
  geom_errorbar(aes(xmin = coef - 1.96 * se, xmax = coef + 1.96 * se), 
                linetype = 1, width = 0, alpha = 0.75, show.legend = F) +
  geom_point(aes(x = coef), size = 2.5, show.legend = F) + 
  scale_color_manual(values = c("grey40", "blue")) +
  scale_shape_manual(values = c(16, 17)) +
  labs(x = "Home-State Effect of Politician Ambassadors\n(Among Top 10 US Export Partners)", 
       y = "",
       title = "") +
  theme_few()


## Figure 5: Home-State Effect and Industry Upstreamness
upstreamness <- read_csv("3digit_upstreamness_by_industry_ACFH2012.csv")

upstreamness <- upstreamness %>%
  na.omit(naics) %>%
  group_by(naics) %>%
  summarize(upstreamness = mean(upstreamness, na.rm = T)) %>%
  arrange(desc(upstreamness))

mod_naics %>%
  mutate(signif_products = ifelse(coef - 1.95 * se > 0, "1", "0")) %>%
  left_join(upstreamness) %>%
  ggplot(aes(x = upstreamness, y = coef)) +
  geom_point(aes(shape = signif_products), alpha = 0.7, size = 2, color = "blue", show.legend = F) +
  geom_smooth(method = "loess", se = F, color = "black") +
  scale_shape_manual(values = c(16, 17)) +
  scale_x_continuous(limits = c(1, 5)) +
  labs(x = "Industry Upstreamness", y = "Politician Ambassador's Home-State Coefficient") +
  theme_few()

home_state_upstreamness <- left_join(mod_naics, upstreamness)
cor.test(home_state_upstreamness$coef, home_state_upstreamness$upstreamness)


### Mechanisms: Information and Electoral Incentives ###

mod.main.exp.p.cst  <- felm(log_export ~ ambassador_home_state * politician * experience | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,],  weights = export_ambassador_cst[export_ambassador_cst$rank <= 10,]$wt_cs)
mod.main.age.p.cst  <- felm(log_export ~ ambassador_home_state * politician * age | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,],  weights = export_ambassador_cst[export_ambassador_cst$rank <= 10,]$wt_cs)

## Table 3: Home-State Effect by Ambassadorial Experience and Age
stargazer(mod.main.exp.p.cst, mod.main.age.p.cst,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes")))











